package org.star.dolphin.utils;

import com.google.common.collect.Maps;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.Map;

public class MapUtil {

    /**
     * map to string
     *
     * @param map
     * @param eqaulsType eqaulsType K与V之间的拼接字符串 = 或者其他...
     * @param spliceType spliceType K-V与K-V之间的拼接字符串  & 或者|...
     * @return
     */
    public static String map2string(Map<String, String> map, String eqaulsType,
                                    String spliceType) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (sb.length() == 0) {
                sb.append(entry.getKey()).append(eqaulsType).append(entry.getValue());
            } else {
                sb.append(spliceType).append(entry.getKey()).append(eqaulsType)
                        .append(entry.getValue());
            }
        }

        return sb.toString();
    }

    /**
     * java bean to map
     * map value is string
     * value null is ""
     *
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> Map<String, String> beanToMapValueString(T bean) {
        Map<String, String> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                Object obj = beanMap.get(key);
                if (!ObjectUtils.isEmpty(obj)) {
                    String value = obj.toString();
                    if (!ObjectUtils.isEmpty(value)) {
                        map.put(key + "", value);
                    }
                }
            }
        }
        return map;
    }

    /**
     * 将对象装换为map
     *
     * @param bean
     * @param discardNull 丢弃空value key-value
     * @param <T>
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean, Boolean discardNull) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                Object value = beanMap.get(key);
                if (discardNull) {
                    if (!ObjectUtils.isEmpty(value)) {
                        map.put(key + "", value);
                    }
                } else {
                    map.put(key + "", value);
                }
            }
        }
        return map;
    }

    /**
     * 将对象装换为map
     * discardNull:false
     *
     * @param bean
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        return beanToMap(bean, false);
    }

    /**
     * 将map装换为javabean对象
     *
     * @param map
     * @param bean
     * @param <T>
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static <T> T mapToBean(Map<String, Object> map, T bean) throws InvocationTargetException, IllegalAccessException {
        /*BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);*/
        BeanUtils.populate(bean,map);
        return bean;
    }

    /**
     * map get key
     *
     * @param map
     * @param v
     * @return
     */
    public static Object getKey(Map<Object, Object> map, String v) {

        Object key = "";

        for (Map.Entry<Object, Object> entry : map.entrySet()) {

            if (entry.getValue().equals(v)) {

                key = entry.getKey();

            }
        }

        return key;

    }

}

/**
 * 重新比较类
 */
class keyComparator implements Comparator<String> {

    @Override
    public int compare(String str1, String str2) {

        return str1.compareTo(str2);
    }

}
